<script setup lang="ts" name="Carousel">
  import { onBeforeUnmount, onMounted, ref } from 'vue';

  const {list,autoplay=false, durcation=1000} = defineProps<{
    list: string[]
    autoplay?: boolean,
    durcation?: number
  }>()

  const active = ref(0)
  // 左箭头
  const hPrev = ()=> {
    active.value--
    if(active.value < 0){
      active.value = list.length-1
    }
  }
  // 右箭头
  const hNext = ()=> {
    active.value++
    if(active.value === list.length){
      active.value = 0
    }
  }

  let timer = -1
  // 开始自动播放
  const start = () => {
    if(autoplay) {
      timer = window.setInterval(()=>{
        hNext()
      },durcation)
    }
  }
  // 停止播放
  const stop = () => {
    clearInterval(timer)
  }
  // 页面一加载就开始自动播放
  onMounted(() => {
    start()
  })
  // 组件销毁时删除定时器
  onBeforeUnmount(() => {
    stop()
  })
</script>

<template>
  <div class="total-carousel" @mouseenter="stop" @mouseleave="start">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{fade: index===active}"
        v-for="(item,index) in list"
        :key="index"
      >
        <img :src="item" alt="">
      </li>
    </ul>
    <a @click="hPrev" href="javascript:;" class="carousel-btn prev">
      <i class="iconfont icon-angle-left"></i></a>
    <a @click="hNext" href="javascript:;" class="carousel-btn next">
      <i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span
        @mouseenter="active = index"
        v-for="(item, index) in list"
        :key="index"
        :class="{active: index===active}"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
  .total-carousel{
    width: 100%;
    height: 100%;
    min-width: 600px;
    min-height: 300px;
    position: relative;

  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      text-decoration: none;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      // top: 228px;
      top:  50%;
      transform: translateY(-50%);
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>